<template>
  <a-card :bordered="false">

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button
        @click="batchDel"
        v-if="selectedRowKeys.length > 0"
        ghost
        type="primary"
        icon="delete">批量删除
      </a-button>
    </div>

    <!-- table区域-begin -->
    <div>

      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i>已选择&nbsp;<a style="font-weight: 600">{{
          selectedRowKeys.length }}</a>项&nbsp;&nbsp;
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        :columns="columns"
        size="middle"
        :pagination="false"
        :dataSource="dataSource"
        :loading="loading"
        rowKey="id"
        :expandedRowKeys="expandedRowKeys"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @expandedRowsChange="handleExpandedRowsChange">

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical"/>
          <a-dropdown>
            <a class="ant-dropdown-link">
              更多 <a-icon type="down"/>
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a href="javascript:;" @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a href="javascript:;" @click="handleAddSub(record)">添加子菜单</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)" placement="topLeft">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>

    </div>
    <!-- table区域-end -->

    <menu-modal ref="modalForm" @ok="modalFormOk"></menu-modal>

  </a-card>
</template>

<script>
  import MenuModal from './modules/MenuModal'
  import { getSystemMenuList } from '@/api/system'
  import { ListMixin } from '@/mixins/ListMixin'

  const columns = [
    {
      title: '菜单名称',
      dataIndex: 'title',
      key: 'title'
    },
    {
      title: 'icon',
      dataIndex: 'icon',
      key: 'icon'
    },
    {
      title: '组件',
      dataIndex: 'component',
      key: 'component',
      scopedSlots: { customRender: 'component' }
    },
    {
      title: '路由',
      dataIndex: 'routeName',
      key: 'routeName'
    },
    {
      title: '路径',
      dataIndex: 'path',
      key: 'path',
      scopedSlots: { customRender: 'path' }
    },
    {
      title: '排序',
      dataIndex: 'sortNum',
      key: 'sortNum'
    },
    {
      title: '操作',
      dataIndex: 'action',
      scopedSlots: { customRender: 'action' },
      align: 'center',
      width: 150
    }
  ]

  export default {
    name: 'MenuList',
    mixins: [ListMixin],
    components: {
      MenuModal
    },
    data () {
      return {
        description: '菜单管理页面',
        // 表头
        columns: columns,
        loading: false,
        // 展开的行，受控属性
        expandedRowKeys: [],
        url: {
          delete: '/menu/',
          deleteBatch: '/menu/batchDelete'
        }
      }
    },
    methods: {
      loadData () {
        this.loading = true
        getSystemMenuList().then((res) => {
          if (res.code === '000000') {
            this.dataSource = res.data
          }
        }).finally(() => {
          this.loading = false
        })
      },
      // 打开数据规则编辑
      handleDataRule (record) {
        this.$refs.PermissionDataRuleList.edit(record)
      },
      handleAddSub (record) {
        this.$refs.modalForm.title = '添加子菜单'
        this.$refs.modalForm.disableSubmit = false
        this.$refs.modalForm.edit({ status: '1', permsType: '1', route: true, 'parentId': record.id, menuType: 1 })
      },
      handleExpandedRowsChange (expandedRows) {
        this.expandedRowKeys = expandedRows
      }
    }
  }
</script>
<style scoped>
</style>
